import Common from "../Common";
import {isEmpty} from "../common/Helpers";
import TeamItem from "./TeamItem";
import More from "../common/More";


const {ccclass, property} = cc._decorator;

@ccclass
export default class Users extends Common {

    @property(cc.Node)
    backBtn: cc.Node;

    @property(cc.Node)
    page: cc.Node = null;

    @property(cc.Node)
    content: cc.Node;

    trItem: cc.Prefab;
    moreItem: cc.Prefab;
    moreNode: cc.Node = null;


    @property(cc.Label)
    reCountsLabel: cc.Label

    @property(cc.Label)
    rePerformanceLabel: cc.Label

    @property(cc.Label)
    teamCountsLabel: cc.Label

    @property(cc.Label)
    teamPerformanceLabel: cc.Label

    @property(cc.Node)
    navContainer: cc.Node


    protected navColor = '#F8D368'
    protected navActiveColor = '#252431'
    protected currentPage = 0; // 当前页码
    protected level = 0;

    protected watch = {
        reUserLists: async (val) => {

        }
    }

    start() {
        this.initEmitter()
        this.getTeamPerformance()
        this.getReMembers()
    }

    async getReMembers(isAppend = true, level = 0) {
        this.level = level;
        try {
            const page = isAppend ? (this.currentPage + 1) : 1;
            const res = await this.http.get('auth/getReMembers', {page, level});
            this.currentPage = res.current_page || this.currentPage
            const data = res.data || []
            if (this.moreNode) {
                this.content.removeChild(this.moreNode)
                this.moreNode = null;
            }
            this.setContent(data, isAppend)
            this.setMoreNode(data.length == (res.per_page || 15))
        } catch (e) {

        }
    }

    protected setContent(data = [], isAppend = true) {
        if (!isAppend) {
            this.clearTr();
        }
        this.attachTr(data)
    }

    protected async getTeamPerformance() {
        try {
            const {data} = await this.http.get('auth/getTeamPerformance', {});
            if (!isEmpty(data)) {
                this.reCountsLabel.string = data.re_counts || 0
                this.rePerformanceLabel.string = data.re_performance || '0.00'
                this.teamCountsLabel.string = data.team_counts || 0
                this.teamPerformanceLabel.string = data.team_performance || '0.00'
            }
        } catch (e) {

        }
    }

    protected initEmitter() {
        this.backBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            cc.director.loadScene('profile')
        }, this) // 点击返回按钮

        // 导航按钮
        this.navContainer.children.forEach((item, index) => {
            item.on(cc.Node.EventType.TOUCH_START, () => {
                this.onNavClick(index)
            }, this)
        })
    }

    protected onNavClick(index: number) {
        this.onBtnClick()
        this.navContainer.children.forEach((item, i) => {
            if (i === index) {
                item.getComponent(cc.Sprite).enabled = true
                const label = item.getChildByName('label')
                label.active = false
                label.color = cc.Color.fromHEX(cc.color(), this.navActiveColor)
                label.active = true
            } else {
                item.getComponent(cc.Sprite).enabled = false
                const label = item.getChildByName('label')
                label.color = cc.Color.fromHEX(cc.color(), this.navColor)
            }
        });
        this.getReMembers(false, index)
    }

    async attachTr(data: any[]) {
        if (!data.length) {
            return;
        }
        const teamItemPrefab = await this.getTrItem();
        data.forEach(item => {
            const teamNode = cc.instantiate(teamItemPrefab)
            const team = teamNode.getComponent(TeamItem)
            team.setUserName(item.user_id)
            team.setLevelName(item.invest_level_name)
            team.setAmounts(item.team_performance)
            team.setNumber(item.team_nums)
            team.setTime(item.created_at)
            this.content.addChild(teamNode)
        })

    }

    clearTr() {
        this.content.removeAllChildren(true)
    }

    async getTrItem() {
        if (!this.trItem) {
            this.trItem = await this.getPrefab('table/teamItem')
        }
        return this.trItem
    }

    async setMoreNode(hasMore = true) {
        const moreNode = await this.getMoreNode()
        const more = moreNode.getComponent(More)
        more.setHasMore(hasMore)
        if (!more.getCallback()) {
            more.setClickCallback(() => {
                this.onBtnClick()
                this.getReMembers(true, this.level)
            })
        }
        this.content.addChild(moreNode)
    }

    async getMoreNode() {
        if (this.moreNode) {
            this.content.removeChild(this.moreNode)
        }
        this.moreNode = cc.instantiate(await this.getMoreItem())
        return this.moreNode
    }

    async getMoreItem() {
        if (!this.moreItem) {
            this.moreItem = await this.getPrefab('more')
        }
        return this.moreItem
    }
}
